package com.example.xunua_mymusic;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.xunua_mymusic.entitys.UserBean;
import com.example.xunua_mymusic.utils.MySqliteHelper;
import com.example.xunua_mymusic.utils.Sqlite_utils;

public class LoginActivity extends AppCompatActivity {
    Button btn_login,btn_register;
    ImageView img_back;
    EditText et_name,et_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        UserBean bean= (UserBean) getIntent().getSerializableExtra("bean");
        if (bean != null) {
            et_name.setText(bean.getName());
            et_pwd.setText(bean.getPassword());
        }
    }
    void init(){
        btn_login=findViewById(R.id.login_btn_login);
        btn_register=findViewById(R.id.login_btn_register);
        et_name=findViewById(R.id.login_et_name);
        et_pwd=findViewById(R.id.login_et_pwd);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sqlite_utils sqlite_utils=new Sqlite_utils();
                MySqliteHelper helper=new MySqliteHelper(LoginActivity.this,1);
                String s = sqlite_utils.checkNmae(helper, et_name.getText().toString().trim());
                if ("此用户名不存在".equals(s)) {
                    Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    if(et_pwd.getText().toString().trim().equals(s)){
                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                        intent.putExtra("username",et_name.getText().toString().trim());
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        img_back=findViewById(R.id.login_img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent=new Intent(LoginActivity.this,WelcomeActivity.class);
//                startActivity(intent);
//                finish();
//                System.exit(0);
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}
